package com.qdsg.ysg.doctor.ui;

import android.graphics.Canvas;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.qdsg.ysg.doctor.util.DpUtil;
import com.qdsg.ysg.doctor.util.SoftKeyboardUtil;
import com.rest.business.RestProxy;
import com.rest.response.Patient;
import com.rest.response.PatientListResponse;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.footer.BallPulseFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import com.qdsg.ysg.doctor.R;
import com.qdsg.ysg.doctor.base.BaseActivity;

/**
 * Created by HP on 2020/2/20.
 */

public class SearchPatientActivity extends BaseActivity {

    @BindView(R.id.refreshLayout)
    RefreshLayout refreshLayout;
    @BindView(R.id.doctor_recyclerView)
    RecyclerView doctor_recyclerView;
    @BindView(R.id.edt_search)
    EditText edt_search;
    List<Patient> list = new ArrayList<>();
    DoctorAdapter doctorAdapter;
    int diagType;
    int current = 1;


    @Override
    protected int getLayoutResource() {
        return R.layout.aty_search_patient;
    }

    @Override
    protected void initViews() {
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        doctor_recyclerView.setLayoutManager(manager);
        doctorAdapter = new DoctorAdapter();
        doctor_recyclerView.setAdapter(doctorAdapter);
        doctor_recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
                super.onDraw(c, parent, state);
            }

            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                outRect.bottom = DpUtil.dip2px( SearchPatientActivity.this, 16);
            }
        });
        refreshLayout.setEnableLoadMore(false);
        refreshLayout.setOnRefreshListener(refreshlayout -> {
            list.clear();
            getDoctorList(edt_search.getText().toString());
        });
        refreshLayout.setEnableLoadMore(false);
        edt_search.setOnEditorActionListener((v, actionId, event) -> {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                // 按下完成按钮，这里和上面imeOptions对应
                getDoctorList(edt_search.getText().toString());

                SoftKeyboardUtil.hideSoftKeyboard( SearchPatientActivity.this);
            }
            return false;
        });
    }

    @Override
    protected void initData() {
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    class DoctorAdapter extends RecyclerView.Adapter<DoctorAdapter.Holder> {

        @Override
        public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
            Holder myHolder;
            myHolder = new Holder(LayoutInflater.from( SearchPatientActivity.this).inflate(R.layout.item_layout, parent, false));
            return myHolder;
        }

        @Override
        public void onBindViewHolder(Holder holder, final int position) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Bundle bundle = new Bundle();
                    bundle.putString("id", list.get(position).id);
                    startActivity( PatientDetailActivity.class, bundle);
                }
            });
            holder.tvName.setText(list.get(position).patientName);
            holder.tv_detail.setText(list.get(position).sex + " " + list.get(position).age);
            holder.tv_message.setText("最近主诊断：" + list.get(position).diagnoseName);
            holder.tv_time.setText(list.get(position).updateTime);
        }

        @Override
        public int getItemCount() {
            return list.size();
        }

        class Holder extends RecyclerView.ViewHolder {

            public TextView tvName, tv_detail, tv_message, tv_time;
            ImageView img_head;

            public Holder(View itemView) {
                super(itemView);
                tvName = itemView.findViewById(R.id.tv_name);
                tv_detail = itemView.findViewById(R.id.tv_detail);
                tv_message = itemView.findViewById(R.id.tv_message);
                tv_time = itemView.findViewById(R.id.tv_time);
            }
        }
    }

    private void getDoctorList(String condition) {
        RestProxy.getInstance().searchPatientList(condition, new Observer<PatientListResponse>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(PatientListResponse patientListResponse) {
                list.clear();
                list.addAll(patientListResponse.data);
                doctorAdapter.notifyDataSetChanged();
            }

            @Override
            public void onError(Throwable e) {
                refreshLayout.finishRefresh(false);
            }

            @Override
            public void onComplete() {
                refreshLayout.finishRefresh(true);
            }
        });
    }


}
